﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;


namespace SSRS_Plus
{
    public class ServersSection : ConfigurationSection
    {
        [ConfigurationProperty("servergroups", Options = ConfigurationPropertyOptions.IsRequired)]
        public servergroups servergroups
        {
            get
            {
                return (servergroups)this["servergroups"];
            }
        }
    }

    [ConfigurationCollection(typeof(clsServers), AddItemName = "server", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class servergroups : ConfigurationElementCollection
    {
        //public servergroups()
        //{
        //    clsServers myElement = (clsServers)CreateNewElement();
        //    Add(myElement);
        //}

        protected override ConfigurationElement CreateNewElement()
        {
            return new clsServers();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            return ((clsServers)element).ServerName;
        }

        [ConfigurationProperty("default", IsRequired = false)]
        public string Default
        {
            get
            {
                return (string)base["default"];
            }
        }

        public void Add(clsServers clsServers)
        {
            BaseAdd(clsServers);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        new public clsServers this[string Name]
        {
            get
            {
                return (clsServers)BaseGet(Name);
            }
        }

        public clsServers this[int index]
        {
            get { return (clsServers)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }


    }

    // [ConfigurationCollection(typeof(EntryElement), AddItemName = "entry", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class clsServers : ConfigurationElement
    {
        /// <summary>
        /// Get or Set Server Name
        /// </summary>
        [ConfigurationProperty("ServerName", IsRequired = true, IsKey = true)]
        public string ServerName
        {
            get
            {
                return this["ServerName"].ToString();
            }
            set
            {
                this["ServerName"] = value;
            }
        }

        /// <summary>
        /// Get or Set SSRS Web Service URL
        /// </summary>
        [ConfigurationProperty("ServerURL", IsRequired = true)]
        public Uri ServerURL
        {
            get { return new Uri(this["ServerURL"].ToString()); }
            set { this["ServerURL"] = value; }
        }

        /// <summary>
        /// Get or Set Authnetication Mode
        /// </summary>
        [ConfigurationProperty("WindowsAuthentication", IsRequired = true)]
        public bool WindowsAuthentication
        {
            get { return (bool)this["WindowsAuthentication"]; }
            set { this["WindowsAuthentication"] = value; }
        }

        /// <summary>
        /// Get or Set Domain Name
        /// </summary>
        [ConfigurationProperty("Domain", IsRequired = false)]
        public string Domain
        {
            get { return this["Domain"].ToString(); }
            set { this["Domain"] = value; }
        }

        /// <summary>
        /// Get or Set User Name
        /// </summary>
        [ConfigurationProperty("UserName", IsRequired = false)]
        public string UserName
        {
            get { return this["UserName"].ToString(); }
            set { this["UserName"] = value; }
        }

        /// <summary>
        /// Get or Set Password
        /// </summary>
        [ConfigurationProperty("Password", IsRequired = false)]
        public string Password
        {
            get { return Security.Decrypt(this["Password"].ToString()); }
            set { this["Password"] = Security.Encrypt(value); }
        }

        /// <summary>
        /// Get or Set Server Mode
        /// </summary>
        [ConfigurationProperty("ServerMode", IsRequired = true)]
        public string ServerMode
        {
            get
            {
                return this["ServerMode"].ToString();
            }
            set
            {
                this["ServerMode"] = value;
            }
        }
    }
}
